package com.hhh.tools.ui.screens

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.text.TextUtils
import android.util.Log
import android.widget.Toast
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.combinedClickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavController
import androidx.navigation.compose.rememberNavController
import com.hhh.tools.ui.components.AppTitle
import com.hhh.tools.ui.navigation.Destinations
import com.hhh.tools.ui.theme.ComposeDemoTheme
import com.hhh.tools.viewmodel.Md5ViewModel

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun Md5Page(navController: NavController) {
    ComposeDemoTheme {
        Column(
            Modifier
                .background(MaterialTheme.colors.background)
                .fillMaxSize()
        ) {
            val context = LocalContext.current

            val model = viewModel<Md5ViewModel>()
            val text: String by model.input.collectAsState("")
            val content: String by model.content.collectAsState("")

            AppTitle("MD5加密") {
                navController.popBackStack()
            }
            Spacer(modifier = Modifier.height(10.dp))

            InputContent(text, "输入需要MD5加密的内容", onValueChange = {
                model.onInputChange(it)
            })

            Text(text = "32位小写", modifier = Modifier.fillMaxWidth().padding(14.dp, 10.dp))

            Text(
                text = if (!TextUtils.isEmpty(content)) content else "",
                modifier = Modifier
                    .fillMaxWidth()
                    .heightIn(100.dp)
                    .padding(horizontal = 14.dp)
                    .combinedClickable(onDoubleClick = {
                        navController.navigate(Destinations.CopyContentPage.route + "/${content}")
                    }, indication = null, interactionSource = remember {
                        MutableInteractionSource()
                    }) {
                    }
                    .background(color = Color.White, shape = RoundedCornerShape(7.dp))
                    .padding(10.dp, 5.dp)
            )

            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(0.dp, 10.dp, 14.dp, 0.dp),
                horizontalArrangement = Arrangement.End
            ) {
                if (!TextUtils.isEmpty(text)) {
                    OptionBtn("清空") {
                        model.onInputChange("")
                    }
                }
                Spacer(modifier = Modifier.width(14.dp))
                OptionBtn("粘贴") {
                    val cm = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
                    val clip = cm.primaryClip
                    if (null != clip && clip.itemCount > 0) {
                        val coerceToText = clip.getItemAt(0).coerceToText(context)
                        if (!TextUtils.isEmpty(coerceToText)) {
                            model.onInputChange(coerceToText.toString().trim())
                        } else {
                            Toast.makeText(context, "内容为空", Toast.LENGTH_SHORT).show()
                        }
                    }
                }
                Spacer(modifier = Modifier.width(14.dp))
                OptionBtn("复制") {
                    if (TextUtils.isEmpty(content)) {
                        Toast.makeText(context, "内容为空", Toast.LENGTH_SHORT).show()
                    } else {
                        Log.e("hhh", content)
                        val cm = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
                        cm.setPrimaryClip(ClipData.newPlainText(context.packageName, content))
                        Toast.makeText(context, "复制成功", Toast.LENGTH_SHORT).show()
                    }
                }
            }
        }
    }
}

@Preview
@Composable
fun Md5PagePreview() {
    val navController = rememberNavController()
    Md5Page(navController)
}

